<template>
  <PrivacyPopup class="get-phone-privacy"></PrivacyPopup>
  <button open-type="getPhoneNumber" class="rechoose-login" @getphonenumber="getPhoneNumber">
    {{ $t('Login.AuthorizePhone.Button.Text') }}
  </button>
  <!--防抖动遮罩-->
  <view style="position: absolute; top: 0; left: 0; width: 100vw; height: 100vh" v-show="apiActing"></view>
  <Modal v-model:visible="visible" :title="$t('APP.Common.Tips.Text')" :ok-text="$t('APP.Common.ISee.Text')">
    <text>{{ dialogContent }}</text>
  </Modal>
</template>
<script setup lang="ts">
import { useAuthStore } from '@/store/auth'
import '@/pluginHelper/pluginHelper'
import { actionDebounce } from '@/utils/mpHelper'
import Modal from '@/common/modal.vue'
import { useI18n } from 'vue-i18n'
import PrivacyPopup from '@/pages/navigator/components/privacyPopup.vue'
let apiActing: boolean = false
const visible = ref(false)
const dialogContent = ref('')
const i18n = useI18n()
const privacyShow = ref(false)
const emit = defineEmits<{
  (e: 'getPhoneNo', code: string): void
}>()

/**
 * 防抖动
 */
function throttle() {
  if (apiActing) return
  apiActing = true
}

const getPhoneNumber = actionDebounce({
  delay: 1000,
  call: async (d: WechatMiniprogram.CustomEvent<{ code: string; errMsg: string }>) => {
    if (d.detail.errMsg.indexOf('ok') <= -1) {
      visible.value = true
      dialogContent.value = i18n.t('Login.Dialog.Content.Authorize.Text')
      return
    }
    uni.showLoading({
      mask: true,
      title: '登录中',
    })
    emit('getPhoneNo', d.detail.code)
  },
  catch: () => {
    visible.value = true
    dialogContent.value = i18n.t('Login.Dialog.Content.GetPhoneFail.Text')
  },
  finally: () => {
    apiActing = false
  },
})

function showPrivacyModal() {
  console.error('showPrivacy---')
  privacyShow.value = true
  console.error('privacyShow', privacyShow.value)
}

function agree() {
  privacyShow.value = false
}

function disagree() {
  privacyShow.value = false
}
</script>
<style lang="less">
.rechoose-login {
  top: 0;
  display: inline-block;
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  background-color: #2d3746;
  box-sizing: border-box;
  font-size: 32rpx;
  text-align: center;
  color: #fff;
  border-radius: 0;
  box-sizing: border-box;
}
</style>
